package com.ctvit.cch.core.enums;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.Getter;

/**
 * 媒体类型枚举
 * 
 * @author heyingcheng
 * @date 2025/11/07
 */
@Getter
public enum MimeTypeEnum {

    // 图片类型
    JPG("jpg", "image/jpeg", FileTypeEnum.IMAGE),
    JPEG("jpeg", "image/jpeg", FileTypeEnum.IMAGE),
    PNG("png", "image/png", FileTypeEnum.IMAGE),
    GIF("gif", "image/gif", FileTypeEnum.IMAGE),

    // 文档类型
    PDF("pdf", "application/pdf", FileTypeEnum.FILE),
    DOC("doc", "application/msword", FileTypeEnum.FILE),
    DOCX("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", FileTypeEnum.FILE),
    XLS("xls", "application/vnd.ms-excel", FileTypeEnum.FILE),
    XLSX("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", FileTypeEnum.FILE),
    CSV("csv", "text/csv", FileTypeEnum.FILE),
    PPT("ppt", "application/vnd.ms-powerpoint", FileTypeEnum.FILE),
    PPTX("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation", FileTypeEnum.FILE),
    ZIP("zip", "application/zip", FileTypeEnum.FILE),
    TXT("txt", "text/plain", FileTypeEnum.FILE),
    MD("md", "text/markdown", FileTypeEnum.FILE),
    LOG("log", "text/plain", FileTypeEnum.FILE),
    HTML("html", "text/html", FileTypeEnum.FILE);

    private final String extension;
    private final String mimeType;
    private final FileTypeEnum fileType;

    private static final Map<String, MimeTypeEnum> EXTENSION_MAP = new LinkedHashMap<>();
    private static final Map<FileTypeEnum, List<MimeTypeEnum>> TYPE_MAP = new LinkedHashMap<>();

    static {
        for (MimeTypeEnum mime : values()) {
            EXTENSION_MAP.put(mime.getExtension().toLowerCase(), mime);
        }

        // 按文件类型分组
        Arrays.stream(values()).collect(Collectors.groupingBy(MimeTypeEnum::getFileType)).forEach(TYPE_MAP::put);
    }

    MimeTypeEnum(String extension, String mimeType, FileTypeEnum fileType) {
        this.extension = extension;
        this.mimeType = mimeType;
        this.fileType = fileType;
    }

    /**
     * 根据扩展名获取MIME类型
     */
    public static String getMimeTypeByExtension(String extension) {
        MimeTypeEnum mime = getByExtension(extension);
        return mime != null ? mime.getMimeType() : null;
    }

    /**
     * 根据扩展名获取枚举
     */
    public static MimeTypeEnum getByExtension(String extension) {
        if (extension == null) {
            return null;
        }
        return EXTENSION_MAP.get(extension.toLowerCase());
    }

    /**
     * 判断是否为支持的文件类型
     */
    public static boolean isSupportedExtension(String extension) {
        return getByExtension(extension) != null;
    }

    /**
     * 判断是否为支持的文件类型
     */
    public static boolean isSupportedImage(String extension) {
        return isSupportedExtension(extension, FileTypeEnum.IMAGE);
    }

    /**
     * 判断是否为支持的文件类型
     */
    public static boolean isSupportedExtension(String extension, FileTypeEnum fileTypeEnum) {
        MimeTypeEnum mimeTypeEnum = getByExtension(extension);
        if (mimeTypeEnum != null) {
            return fileTypeEnum == mimeTypeEnum.getFileType();
        }
        return false;
    }

    /**
     * 获取所有支持的扩展名
     */
    public static List<String> getAllExtensions() {
        return new ArrayList<>(EXTENSION_MAP.keySet());
    }

    /**
     * 获取指定类型的扩展名
     */
    public static List<String> getExtensionsByFileType(FileTypeEnum fileType) {
        List<MimeTypeEnum> mimeTypeEnums = TYPE_MAP.get(fileType);
        if (mimeTypeEnums == null) {
            return Collections.emptyList();
        }
        return mimeTypeEnums.stream()
                .map(MimeTypeEnum::getExtension)
                .collect(Collectors.toList());
    }

}
